<?php

class FormBuilder
{

	private $errors;

	function __construct($errors = null)
	{
		if (isset($errors))
		{
			$this->errors=$errors;
		}
		else
		{
			$this->errors=array();
		}
	}

	function getFormValue($name)
	{
		// Assume no value already exists:
		$value = false;

		if (isset($_SESSION[$name])) $value = $_SESSION[$name];

		if (isset($_POST[$name])) $value = $_POST[$name];
			
		if (isset($_GET[$name])) $value = $_GET[$name];

		// Strip slashes if Magic Quotes is enabled:
		if ($value && get_magic_quotes_gpc()) $value = stripslashes($value);
			
		return $value;
			
	}

	function createTextInput($name,$cssClass="input-large")
	{
		if($this->getFormValue($name))
		$value=$this->getFormValue($name);
		else
		$value='';
		return '<input id="'.$name.'" name="'.$name.'" type="text" class="'.$cssClass.'" value="'.htmlspecialchars($value).'" />';
		 
	}
	
	function createCheckBoxInput($name,$value,$cssClass="")
	{
		
		if($this->getFormValue($name))
		{
			$selValue=$this->getFormValue($name);
			if($selValue==$value)
			{
				return '<input id="'.$name.'" name="'.$name.'" type="checkbox" class="'.$cssClass.'" value="'.htmlspecialchars($value).'" checked="true"/>';
			}
		}	
		
		return '<input id="'.$name.'" name="'.$name.'" type="checkbox" class="'.$cssClass.'" value="'.htmlspecialchars($value).'" />';
		 
	}
	
	function createRadioBoxInput($name,$value,$checked=false,$cssClass="")
	{
		
		if($this->getFormValue($name))
		{
			$selValue=$this->getFormValue($name);
			if($selValue==$value)
			{
				return '<input id="'.$name.'" name="'.$name.'" type="radio" class="'.$cssClass.'" value="'.htmlspecialchars($value).'" checked="true"/>';
			}
		}
		elseif($checked)
		{
			return '<input id="'.$name.'" name="'.$name.'" type="radio" class="'.$cssClass.'" value="'.htmlspecialchars($value).'" checked="true"/>';	
		}	
		
		return '<input id="'.$name.'" name="'.$name.'" type="radio" class="'.$cssClass.'" value="'.htmlspecialchars($value).'" />';
		 
	}
	
	function decorateInputByPrePendIcon($input,$icon=null,$cssClass="input-prepend")
	{
		if(isset($icon))
		return '<div class="'.$cssClass.'"><span class="add-on"><i class="'.$icon.'"></i></span>'.$input.'</div>';
		else
		return $input;
	}

	function decorateInputByInlineHelp($input,$name,$help=null,$cssClass="help-inline")
	{
		if (array_key_exists($name, $this->errors))
		{
			return $input.'<span class="'.$cssClass.'">' . $this->errors[$name] . '</span>';
		} elseif(isset($help))
		{
			return $input.'<span class="'.$cssClass.'">' . $help . '</span>';
		}else
		{
			return $input;
		}
	}

	function wrapInputToDiv($input,$cssClass=null,$cssId=null)
	{
		$tag='<div ';
		if(isset($cssClass))
		$tag=$tag.'class="'.$cssClass.'"';
		if(isset($cssId))
		$tag=$tag.'class="'.$cssClass.'"';
		 
		return  $tag.'>'.$input.'</div>';
	}
	 
	function decorateInputByLabel($input,$label,$name=null,$cssClass="control-label")
	{
		if(isset($name))			
		return '<label class="'.$cssClass.'" for="'.$name.'">'.$label.'</label>'.$input;
		else 
		return '<label class="'.$cssClass.'" >'.$label.'</label>'.$input;
	}
	
	function decorateCheckBoxInputByLabel($input,$label,$name=null,$cssClass="control-label")
	{
		if(isset($name))			
		return '<label class="'.$cssClass.'" for="'.$name.'">'.$input.$label.'</label>';
		else 
		return '<label class="'.$cssClass.'" >'.$input.$label.'</label>';
	}
	
	function renderTextInput($label,$name,$icon=null,$help=null)
	{
		return $this->wrapInputToDiv(
				$this->decorateInputByLabel($this->wrapInputToDiv(
							$this->decorateInputByInlineHelp(
								$this->decorateInputByPrePendIcon(
									$this->createTextInput($name)
										,$icon),$name,$help),'controls'),$label,$name),'control-group');	
	}
	
	function renderCheckBoxInput($label,$name,$value, $icon=null,$help=null)
	{
		return $this->wrapInputToDiv($this->wrapInputToDiv($this->decorateCheckBoxInputByLabel($this->createCheckBoxInput($name,$value),$label,$name,'checkbox'),'controls'),'control-group');
	}
}